package com.tuanzi.loan.web.bean.family;

import com.tuanzi.loan.business.entity.family.LoanNewCreditCardProgress;
import com.tuanzi.loan.business.service.family.LoanNewCreditCardProgressService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.Objects;

/**
 * User: liangZongXi
 * Create time: 11:31 2018/9/20
 * Description:
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanNewCreditCardProgressInfoBean extends JSFInfoBean<LoanNewCreditCardProgress> {
    private static final long serialVersionUID = -2730610746397105311L;

    @Autowired
    private LoanNewCreditCardProgressService loanNewCreditCardProgressService;

    @PostConstruct
    private void init(){
        String idStr = getParamFromRequest("id");
        int id = NumberUtils.toInt(idStr);
        if(id !=0){
            $ = loanNewCreditCardProgressService.findOne(id);
        }else{
            $ = new LoanNewCreditCardProgress();
            changeStatusToEdit();
        }
    }


    public void save(){
        String bankName = $.getBankName();
        String redirectUrl = $.getRedirectUrl();
        if(StringUtils.isAnyBlank(bankName,redirectUrl)){
            addWarnMessage("银行名称或者跳转链接不能为空");
            return;
        }

        LoanNewCreditCardProgress cardProgressInfo = loanNewCreditCardProgressService.findByBankName(bankName);
        if(Objects.nonNull(cardProgressInfo) && $.getId() == null){
            addErrorMessage("银行已存在");
            return;
        }

        if($.getId() == null){
            $.setStatus(1);
        }

        loanNewCreditCardProgressService.saveOrUpdate($);

        forceRedirect("/views/family/credithousekeeper/credit-progress-list.xhtml");
    }

    public String back(){
        return redirect("/views/family/credithousekeeper/credit-progress-list.xhtml");
    }
}
